package org.grottarossa.ui.core.controls.document;


import java.util.Hashtable;
import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.content.Document;
import org.grottarossa.template.data.pojo.content.DocumentList;
import org.grottarossa.ui.core.i18n.DM;

import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;

/***
 * Rappresenta il container delle versioni di un documento
 * @author 
 *
 */
public class DocumentVersionsContainer extends IndexedContainer
{
	/**
	 *
	 */
	private static final long serialVersionUID = -7019807300426331329L;
	private static ResourceBundle m_localizer = DM.instance().getBundle(DocumentVersionsContainer.class.getName());
	public static String VERSION = m_localizer.getString("header.version");
	public static String NAME = m_localizer.getString("header.name");

	private Hashtable<String, ThemeResource> m_iconsMap = new Hashtable<String, ThemeResource>();

	public DocumentVersionsContainer()
	{
		m_iconsMap.put("txt", DocumentsResources.txt);
		m_iconsMap.put("doc", DocumentsResources.doc);
		m_iconsMap.put("docx", DocumentsResources.doc);
		m_iconsMap.put("jpg", DocumentsResources.image);
		m_iconsMap.put("jpeg", DocumentsResources.image);
		m_iconsMap.put("gif", DocumentsResources.image);
		m_iconsMap.put("png", DocumentsResources.image);
		m_iconsMap.put("bmp", DocumentsResources.image);
		m_iconsMap.put("pdf", DocumentsResources.pdf);
		m_iconsMap.put("ppt", DocumentsResources.ppt);
		m_iconsMap.put("pptx", DocumentsResources.ppt);
		m_iconsMap.put("html", DocumentsResources.web);
		m_iconsMap.put("xls", DocumentsResources.xls);
		m_iconsMap.put("xlsx", DocumentsResources.xls);
	}

	public DocumentVersionsContainer(DocumentList versions)
	{
		addContainerProperty("", Component.class, null);
		addContainerProperty(VERSION, Integer.class, null);
		addContainerProperty(NAME, String.class, null);

		if(versions != null && versions.size() > 0)
			for (Document d: versions)
			{
				Object obj = addItem();
				Resource r = getResourceByFile(d);
				getContainerProperty(obj, "").setValue(new Embedded("", r));
				getContainerProperty(obj, VERSION).setValue(d.getFileVersion());
				getContainerProperty(obj, NAME).setValue(d.getFileName());
			}
		else
		{
			Object obj = addItem();
			getContainerProperty(obj, "").setValue(new Embedded("", DocumentsResources.uploadVersion));
			getContainerProperty(obj, VERSION).setValue(0);
			getContainerProperty(obj, NAME).setValue(m_localizer.getString("cells.values.inprogress"));
		}
	}

	private ThemeResource getResourceByFile(Document d)
	{
		if(d.getFileExtension() != null)
			if(m_iconsMap.contains(d.getFileExtension().toString().toLowerCase()))
					return m_iconsMap.get(d.getFileExtension().toString().toLowerCase());

		return DocumentsResources.defaultDocument;
	}
}
